//package com.bd.scm.listener;
//
//import org.activiti.bpmn.model.*;
//import org.activiti.engine.*;
//import org.activiti.engine.history.HistoricTaskInstance;
//import org.activiti.engine.repository.ProcessDefinition;
//import org.activiti.engine.runtime.Execution;
//import org.activiti.engine.runtime.ExecutionQuery;
//import org.activiti.engine.runtime.ProcessInstance;
//import org.activiti.engine.task.Task;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//
///**
// * @author Mason
// * @version 1.0
// * @since 2019/12/24 10:37
// */
//@Service
//public class MyActivitiFun {
//
//    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private RepositoryService repositoryService;
//    @Autowired
//    private RuntimeService runtimeService;
//    @Autowired
//    private HistoryService historyService;
//    @Autowired
//    private ManagementService managementService;
//
//    /**
//     * 判断当前执行节点之前是否有userTask节点，有true ，无false.
//     *
//     * @param processInstanceId 流程实例ID
//     */
//    public Boolean checkBeforeHaveUserTask(String processInstanceId) {
//        Boolean flag = false;
//        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
//        if(null!=task){
//            // 取得已提交的任务
//            HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery()
//                    .taskId(task.getId()).singleResult();
//            // 取得正在流转的流程实例,若已完成则为null
//            //getRuntimeProcessInstance是自己封装的获取流程实例的方法
//            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId
//                    (processInstanceId).singleResult();
//            //每个流程实例只有一个executionId
//            //获得流程定义
//            ProcessDefinition processDefinition = repositoryService.getProcessDefinition(historicTaskInstance
//                    .getProcessDefinitionId());
//
//            //获得当前流程的活动ID
//            ExecutionQuery executionQuery = runtimeService.createExecutionQuery();
//            Execution execution = executionQuery.executionId(historicTaskInstance.getExecutionId()).singleResult();
//            String activityId = execution.getActivityId();
//            UserTask userTask = null;
//            while (true) {
//                //根据活动节点获取当前的组件信息
//                FlowNode flowNode = getFlowNode(processDefinition.getId(), activityId);
//                //获取该流程组件的之后/之前的组件信息
//                //List<SequenceFlow> sequenceFlowListOutGoing = flowNode.getOutgoingFlows();
//                List<SequenceFlow> sequenceFlowListIncoming = flowNode.getIncomingFlows();
//
//                //sequenceFlowListIncoming数量可能大于1,可以自己做判断,此处只取第一个
//                FlowElement flowElement = sequenceFlowListIncoming.get(0).getTargetFlowElement();
//                if (flowElement instanceof UserTask) {
//                    flag = true;
//                    break;
//                } else {
//                    //下一节点不是任务userTask的任务节点,所以要获取再下一个节点的信息,直到获取到userTask任务节点信息
//                    String flowElementId = flowElement.getId();
//                    activityId = flowElementId;
//                    continue;
//                }
//            }
//        }
//        return flag;
//    }
//
//    /**
//     * 判断当前执行节点之后是否是结束节点  是true ，否false.
//     *
//     * @param processInstanceId
//     */
//    public Boolean checkNextIsEndevent(String processInstanceId) {
//        Boolean flag = false;
//        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
//        // 取得已提交的任务
//        HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery()
//                .taskId(task.getId()).singleResult();
//        // 取得正在流转的流程实例,若已完成则为null
////getRuntimeProcessInstance是自己封装的获取流程实例的方法
//        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId
//                (processInstanceId).singleResult();
//        //每个流程实例只有一个executionId
//        //获得流程定义
//        ProcessDefinition processDefinition = repositoryService.getProcessDefinition(historicTaskInstance
//                .getProcessDefinitionId());
//
//        //获得当前流程的活动ID
//        ExecutionQuery executionQuery = runtimeService.createExecutionQuery();
//        Execution execution = executionQuery.executionId(historicTaskInstance.getExecutionId()).singleResult();
//        String activityId = execution.getActivityId();
//        UserTask userTask = null;
//            //根据活动节点获取当前的组件信息
//            FlowNode flowNode = getFlowNode(processDefinition.getId(), activityId);
//            //获取该流程组件的之后/之前的组件信息
//            List<SequenceFlow> sequenceFlowListOutGoing = flowNode.getOutgoingFlows();
////        List<SequenceFlow> sequenceFlowListIncoming=flowNode.getIncomingFlows();
//
//            //获取的下个节点不一定是userTask的任务节点，所以要判断是否是任务节点
//            //sequenceFlowListOutGoing数量可能大于1,可以自己做判断,此处只取第一个
//            FlowElement flowElement = sequenceFlowListOutGoing.get(0).getTargetFlowElement();
//            if (flowElement instanceof EndEvent) {
//                flag = true;
//            }
//        return flag;
//    }
//
//    public void getNextUserFlowElement(String processInstanceId) {
//        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
//        if (task == null) {
//            System.out.println("流程未启动或已执行完成");
//        } else {
//            // 取得已提交的任务
//            HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery()
//                    .taskId(task.getId()).singleResult();
//            // 取得正在流转的流程实例,若已完成则为null
////getRuntimeProcessInstance是自己封装的获取流程实例的方法
//            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId
//                    (processInstanceId).singleResult();
//            //每个流程实例只有一个executionId
//            //获得流程定义
//            ProcessDefinition processDefinition = repositoryService.getProcessDefinition(historicTaskInstance
//                    .getProcessDefinitionId());
//
//            //获得当前流程的活动ID
//            ExecutionQuery executionQuery = runtimeService.createExecutionQuery();
//            Execution execution = executionQuery.executionId(historicTaskInstance.getExecutionId()).singleResult();
//            String activityId = execution.getActivityId();
//            UserTask userTask = null;
//            while (true) {
//                //根据活动节点获取当前的组件信息
//                FlowNode flowNode = getFlowNode(processDefinition.getId(), activityId);
//                //获取该流程组件的之后/之前的组件信息
//                List<SequenceFlow> sequenceFlowListOutGoing = flowNode.getOutgoingFlows();
////        List<SequenceFlow> sequenceFlowListIncoming=flowNode.getIncomingFlows();
//
//                //获取的下个节点不一定是userTask的任务节点，所以要判断是否是任务节点
//                //sequenceFlowListOutGoing数量可能大于1,可以自己做判断,此处只取第一个
//                FlowElement flowElement = sequenceFlowListOutGoing.get(0).getTargetFlowElement();
//                if (flowElement instanceof UserTask) {
//                    userTask = (UserTask) flowElement;
//
//                    System.out.println(userTask.getAssignee());
//                    System.out.println(userTask.getName());
//                    System.out.println("获取该任务节点的审批信息...");
//                    break;
//                } else {
//                    //下一节点不是任务userTask的任务节点,所以要获取再下一个节点的信息,直到获取到userTask任务节点信息
//                    String flowElementId = flowElement.getId();
//                    activityId = flowElementId;
//                    continue;
//                }
//            }
//        }
//
//
//    }
//
//    //根据活动节点和流程定义ID获取该活动节点的组件信息
//    public FlowNode getFlowNode(String processDefinitionId, String activityId) {
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
//        FlowNode flowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(activityId);
//        return flowNode;
//    }
//
//    public void test(){
//        System.out.println("testtesttest");
//    }
//}
